﻿using Assets.Scripts.Net.Data;
using Assets.Scripts.UI.Panel;
using UnityEngine;

namespace Assets.Scripts.UI
{
    public class UIManager :UIBase<UIManager>
    {
        public Transform UIParent;//UI的父级别
        public Camera MainCamera;//UI主相机

        protected override UIType UIType
        {
            get { return UIType.Null; }
        }

        public static UIManager _Instance;

        protected override void InitOnDestroy()
        {
            _Instance = null;
        }

        protected override void InitOnAwake()
        {
            _Instance = this;
            AutoFindComponent(transform, true);
            Application.runInBackground = true;//允许程序后台运行
        }

        protected override void InitOnStart()
        {
            CreatePanel(PanelType.LoginPanel);
        }

        public void ShowTipInfo(string tipText) 
        {
            Exit("TipPanel(Clone)");
            TipPanel panel= CreatePanel(PanelType.TipPanel).GetComponent<TipPanel>();
            panel.Init(tipText);
        }

        public void ExitPanel(string panelName)
        {
            Transform tsf= UIParent.Find(panelName);
            if (tsf != null)
            {
                Destroy(tsf.gameObject);
            }
        }


        /// <summary>
        /// 登入过期
        /// </summary>
        public void LoginOutDate()
        {
            //清理缓存
            //NetReaderFactory.Instance().CleanAllCacheInfo();
            ExitPanel("LoadingLoopPanel(Clone)");
            ExitPanel("LoadingPanel(Clone)");
            CreatePanel(PanelType.LoginTimeOutPanel);
            ShowTipInfo("登入过期，请重新登入");
        }

        /// <summary>
        /// 登入过期进入考试的时候的特殊处理
        /// </summary>
        public void LoginOutDateOnDoExam()
        {
            ExitPanel("LoadingLoopPanel(Clone)");
            ExitPanel("LoadingPanel(Clone)");
            ExitPanel("ExamRulePanel(Clone)");
            CreatePanel(PanelType.LoginPanel);
            ShowTipInfo("登入过期，请重新登入");
        }

        /// <summary>
        /// 其他用户登入
        /// </summary>
        public void OtherUserLogin()
        {
            ExitPanel("LoadingLoopPanel(Clone)");
            ExitPanel("LoadingPanel(Clone)");
            CreatePanel(PanelType.LoginTimeOutPanel);
            ShowTipInfo("您的账号的异地被登入，请重新登入");
        }

        public void SetCamereState(bool isActive)
        {
            MainCamera.gameObject.SetActive(isActive);
        }
    }
}
